package com.hymake.processor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.hymake.model.Dy2018Repo;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * 电影天堂
 * 
 */
@Component
public class Dy2018Processor implements PageProcessor {
	
    @Qualifier("dy2018DaoPipeline")
    @Autowired
    private Pipeline dy2018DaoPipeline;
    
    public static final String URL_LIST = "https://www.dy2018.com/html/gndy/dyzz/index\\w*\\.html";

    public static final String URL_POST = "https://www.dy2018.com/i/\\d+\\.html";

    private Site site = Site
            .me()
            .setDomain("www.dy2018.com")
            .setSleepTime(3000)
            .setUserAgent(
                    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");

    public void process(Page page) {
        //列表页
        if (page.getUrl().regex(URL_LIST).match()) {
            page.addTargetRequests(page.getHtml().xpath("//div[@class='co_content8']").links().all());
            page.addTargetRequests(page.getHtml().links().regex(URL_LIST).all());
            //详情页
        } else {
            Dy2018Repo dy2018Repo = new Dy2018Repo();
            dy2018Repo.setName(page.getHtml().xpath("//div[@class='co_area2']/div/h1/text()").toString());
            dy2018Repo.setScore(page.getHtml().xpath("//div[@class='co_content8']//strong/text()").toString());
            dy2018Repo.setType(page.getHtml().xpath("//div[@class='co_content8']//span[2]//allText()").toString());
            dy2018Repo.setPublishTime(page.getHtml().xpath("//div[@class='co_content8']//span[3]/text()").regex("\\d+-\\d+-\\d+").toString());
            dy2018Repo.setUrl(page.getHtml().xpath("//div[@id='Zoom']//a/@href").all().toString());
            page.putField("repo", dy2018Repo);
        }
    }
    
    public Site getSite() {
        return site;
    }
	
	public void crawl(PageProcessor jobCrawler) {
		Spider.create(jobCrawler)//
				.addUrl("https://www.dy2018.com/html/gndy/dyzz/index.html")//
				.addPipeline(dy2018DaoPipeline)//
				.thread(5)//
				.run();
	}

	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/spring/applicationContext*.xml");
        final Dy2018Processor jobCrawler = applicationContext.getBean(Dy2018Processor.class);
        jobCrawler.crawl(jobCrawler);
	}


}
